package cz.muni.fi.hauntedhousesweb.web;

import cz.muni.fi.hauntedhouses.service.GhostService;
import cz.muni.fi.hauntedhouses.to.GhostTO;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author Andrea Vasekova, 325181@mail.muni.cz
 */
@UrlBinding("/ghosts/{$event}/{ghost.id}")
public class GhostsActionBean implements ActionBean {
    
    private ActionBeanContext context;
    
    @SpringBean
    private GhostService ghostService;
    
    @ValidateNestedProperties(value = {
    @Validate(on = {"add", "save"}, field = "name", required = true),
    @Validate(on = {"add", "save"}, field = "power", required = true, minvalue = 0, maxvalue = 100),
    @Validate(on = {"add", "save"}, field = "hauntingFrom", mask = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"),
    @Validate(on = {"add", "save"}, field = "hauntingTo", mask = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
    })
    private GhostTO ghost;
    
    private String findThis = "";

    public String getFindThis() {
        return findThis;
    }

    public void setFindThis(String findThis) {
        this.findThis = findThis;
    }

    @DefaultHandler
    public Resolution all() {
        return new ForwardResolution("/showGhosts.jsp");
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
    public List<GhostTO> getGhosts() {
        return ghostService.findAll();
    }
    
    public Resolution add() {
        ghostService.create(ghost);
        return new RedirectResolution(this.getClass(), "all");
    }
 
    public GhostTO getGhost() {
        return ghost;
    }

    public void setGhost(GhostTO ghost) {
        this.ghost = ghost;
    }
    
    public Resolution delete() {
        ghostService.remove(ghost);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadGhostFromDatabase() {
        String ids = context.getRequest().getParameter("ghost.id");
        if (ids != null) {
            ghost = ghostService.get(Long.parseLong(ids));
        }
    }
 
    public Resolution edit() {
        return new ForwardResolution("/editGhost.jsp");
    }
 
    public Resolution save() {
        ghostService.update(ghost);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution cancel() {
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution search() {
        return new ForwardResolution("/searchGhosts.jsp");
    }
    
    public List<GhostTO> getGhostsWithName() {
        //hack
        if (findThis == null) {
            findThis = "";
        }
        return ghostService.findByName(findThis);
    }
    
}
